package GUI;

import jsonObjects.Profile;
import org.apache.log4j.Logger;

import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;

import serverHandling.SessionObjects;

import javax.swing.*;

import java.util.List;

public class ProfilesEditGUI extends javax.swing.JFrame {
    private static Logger log = Logger.getLogger(ProfilesEditGUI.class);

    public ProfilesEditGUI() {
	initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
	try {
	    UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
	} catch (Exception e) {
	    log.error(e.getMessage());
	}
	List<Profile> activProfTab = SessionObjects.getProfiles();
	Object[][] futureMenuItems;
	if (activProfTab != null) {
	    futureMenuItems = new Object[activProfTab.size()][activProfTab
		    .size()];
	    for (int i = 0; i < activProfTab.size(); i++) {
		futureMenuItems[i][0] = activProfTab.get(i).getName();
	    }
	} else
	    futureMenuItems = new Object[0][0];

	jScrollPane1 = new javax.swing.JScrollPane();
	jTable1 = new javax.swing.JTable();
	jButton1 = new javax.swing.JButton();
	jButton2 = new javax.swing.JButton();

	setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

	jTable1.setModel(new javax.swing.table.DefaultTableModel(
		futureMenuItems, new String[] { "Nazwa" }) {
	    Class[] types = new Class[] { java.lang.String.class };
	    boolean[] canEdit = new boolean[] { false };

	    public Class getColumnClass(int columnIndex) {
		return types[columnIndex];
	    }

	    public boolean isCellEditable(int rowIndex, int columnIndex) {
		return canEdit[columnIndex];
	    }
	});
	jTable1.setColumnSelectionAllowed(true);
	jTable1.getTableHeader().setReorderingAllowed(false);
	jScrollPane1.setViewportView(jTable1);
	jTable1.getColumnModel()
		.getSelectionModel()
		.setSelectionMode(
			javax.swing.ListSelectionModel.SINGLE_SELECTION);
	jTable1.getColumnModel().getColumn(0).setResizable(false);

	jButton1.setText("View profile attributes");
	jButton1.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton1ActionPerformed(evt);
	    }
	});

	jButton2.setText("Cancel");
	jButton2.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton2ActionPerformed(evt);
	    }
	});

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.TRAILING)
						.addGroup(
							layout.createSequentialGroup()
								.addComponent(
									jButton2)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									jButton1))
						.addComponent(
							jScrollPane1,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							375,
							javax.swing.GroupLayout.PREFERRED_SIZE))
				.addContainerGap(
					javax.swing.GroupLayout.DEFAULT_SIZE,
					Short.MAX_VALUE)));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jScrollPane1,
					javax.swing.GroupLayout.PREFERRED_SIZE,
					240,
					javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(jButton1)
						.addComponent(jButton2))
				.addContainerGap(15, Short.MAX_VALUE)));

	jButton1.getAccessibleContext().setAccessibleName("okButton");
	jButton2.getAccessibleContext().setAccessibleName("anulujButton");

	pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	if (jTable1.getSelectedRowCount() == 1) {

	    int row = jTable1.getSelectedRow();
	    int column = jTable1.getSelectedColumn();
	    String urObjctInCell = (String) jTable1.getValueAt(row, column);
	    final Profile profTmp = SessionObjects
		    .getProfileByName(urObjctInCell);

	    if (profTmp == null)
		JOptionPane.showMessageDialog(null,
			"Nie odnaleziona atrybutow dla wybranego profilu!");

	    else {
		this.setVisible(false);
		java.awt.EventQueue.invokeLater(new Runnable() {
		    public void run() {
			new AttributesPropertiesGUI(profTmp.getId())
				.setVisible(true);
		    }
		});
	    }

	} else {
	    JOptionPane.showMessageDialog(null, "Nalezy wybrac profil!");
	}
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
	this.setVisible(false);
    }

    public static void main(String args[]) {
	java.awt.EventQueue.invokeLater(new Runnable() {

	    public void run() {
		new ProfilesEditGUI().setVisible(true);
	    }
	});
    }

    javax.swing.JButton jButton1;
    javax.swing.JButton jButton2;
    javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
}
